package com.example.dao;

import com.example.pojo.Consult;
import com.example.pojo.TestPaper;
import com.example.pojo.TestQuestion;
import com.example.pojo.TrainingPlan;
import com.example.pojo.vo.TestPaperForTestVo;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface TestPaperCrudRepository extends CrudRepository<TestPaper, Integer>, JpaSpecificationExecutor<TestPaper> {
    // 查询与特定培训计划关联的试题（已选试题）
    @Query("SELECT tq FROM TestQuestion tq WHERE tq.id IN (SELECT tp.testQuestionId FROM TestPaper tp WHERE tp.trainingPlanId = :trainingPlanId)")
    List<TestQuestion> selectedQuestion(Integer trainingPlanId);

    // 查询与特定培训计划未关联的试题（未选试题）
    @Query("SELECT tq FROM TestQuestion tq WHERE tq.id NOT IN (SELECT tp.testQuestionId FROM TestPaper tp WHERE tp.trainingPlanId = :trainingPlanId) AND (tq.agencyId = 0 OR tq.agencyId = :agencyId)")
    List<TestQuestion> unselectedQuestion(Integer trainingPlanId, Integer agencyId);

    void deleteByTestQuestionIdAndTrainingPlanId(Integer testQuestionId, Integer trainingPlanId);


    TestPaper getTestPaperByTrainingPlanIdAndTestQuestionId(Integer trainingPlanId, Integer testQuestionId);



    @Query("SELECT new com.example.pojo.vo.TestPaperForTestVo(tq, tp.score) " +
            "FROM TestQuestion tq " +
            "JOIN TestPaper tp ON tq.id = tp.testQuestionId " +
            "WHERE tp.trainingPlanId = :trainingPlanId")
    List<TestPaperForTestVo> selectedQuestionAndScore(Integer trainingPlanId);
}
